import { RequestFunctionParams } from 'yapi-to-typescript';
import { fetchFn, IRequestInit } from './fetch';

export default function request<TResponseData>(
  payload: RequestFunctionParams,
  options: IRequestInit = {}
): Promise<TResponseData> {
  // 具体请求逻辑
  const body = ['PUT', 'POST'].includes(payload.method) ? payload.data : undefined;
  // 发起请求
  return fetchFn<TResponseData>(payload.path, {
    method: payload.method.toLowerCase(),
    body,
    paramNames: payload.paramNames,
    ...options
  });
}
